/**
 * Created by ivancai on 2017/7/24.
 */


//提示框
function TipLayer (sCont,sStyle,fn,T) {
    layer.open({
        content: sCont,
        style:sStyle != 'no' ? sStyle : 'background-color:rgba(255,255,255,1); color:#252525; border:none; border-radius: 5px;',
        scrollbar: false,
        time: T || 2,
        shadeClose: true,
        success:function() {if(fn)fn();}
    });
}

var minCount=$('#minCount').val()*1;
if(minCount==null || minCount=="undefined"){
    minCount=1;
}
// 最高限售份数 maxCount为0时是不限购
var limitSize = 99;
var maxCount = $('#maxCount').val()*1;
if(maxCount != null || maxCount != "undefined"){
    if(maxCount != 0 && maxCount <= limitSize){
        limitSize = maxCount
    }else{
        limitSize = 99
    }
}

// 税率
var taxRare = 0;
var taxCount = 0;
// 计算初始化的显示税费
if($('#taxPrize') && $('#taxPrize').length>0){
    taxRare = $('#taxPrize').parents('.js_taxRare').siblings('.top_div2').find('li.on').attr('taxFare');
    if(taxRare && taxRare!= undefined && taxRare != ""){
        taxCount = (taxRare*minCount).toFixed(2)*1;
    }else{
        taxCount = 0;
    }
    $('#taxPrize').text(taxCount)
}

var buyCount = minCount, curPrice = $('#price').text(), originPrice = curPrice, curProductId = $('#proId').val(), curStore = $('#store').text()
// setDefaultUI()
function onChooseItem(productId, name, store, price, imgPath, taxFareLi, li) {
    if($(li).hasClass('noStore')) {
        TipLayer("该商品已经没有库存啦~");
        return;
    }
    originPrice = curPrice = price, buyCount = minCount, curProductId = productId, curStore = store
    if(taxFareLi && taxFareLi != '' && taxFareLi != undefined){
        taxRare = taxFareLi*1;
        taxCount = (taxFareLi*buyCountTg).toFixed(2)*1;
    }else{
        taxRare = 0;
        taxCount = 0
    }
    price = price*1;
    curPrice = curPrice*1;
    updateBuyCountUI()
    changeCurStatus(name, store, price, taxCount, imgPath)
    $('.productItem').removeClass("on")
    $(li).addClass("on")
}
function changeCurStatus(name, store, price, taxPrize, imgPath) {
    $('#proName').text(name)
    $('#store').text(store)
    $('#price').text(price)
    $('#taxPrize').text(taxPrize);
    if( imgPath != ""){
        $('#proImage').attr("src", imgPath)
    }

}
function setDefaultUI() {
    $($('.productItem')[0]).addClass("on")
}
function onPlus() {
    if (buyCount < limitSize) {
        if (buyCount >= curStore) {
            TipLayer("选购数量不能超过库存哦~");
            return
        }
        buyCount++
        taxCount = (buyCount * taxRare).toFixed(2)*1;
        curPrice = (buyCount * originPrice).toFixed(2)*1;

        updateBuyCountUI()
    }else{
        TipLayer("最多只能"+limitSize+"，不要太贪心哦");
    }
}
function onReduce() {
    if (buyCount <= minCount){
        TipLayer("不能再少了~");
        return
    }
    buyCount--
    taxCount = (buyCount * taxRare).toFixed(2)*1;
    curPrice = ( buyCount * originPrice).toFixed(2)*1;
    updateBuyCountUI()
}
function updateBuyCountUI() {
    $('#buyCount').val(buyCount);
    $('#price').text(curPrice);
    $('#taxPrize').text(taxCount)
}
$("#buyCount").bind('input porpertychange', function () {
    var input = $("#buyCount").val();
    // 库存与最小限制作比较
    if(parseInt(curStore)<minCount){
        TipLayer("库存不足~");
    }else{
        if (input <= minCount) {
            TipLayer("不能再少了~");
            $("#buyCount").val(minCount)
        }
    }

    // 库存与最大限制作比较
    if(parseInt(curStore)>=limitSize){
        if(input >= limitSize){
            TipLayer("最多只能"+limitSize+"，不要太贪心哦");
            $("#buyCount").val(limitSize)
        }
    }else{
        if(input >= parseInt(curStore)){
            TipLayer("选购数量不能超过库存哦~");
            $("#buyCount").val(curStore);
            input = $("#buyCount").val();
            buyCount = input;
            taxCount = (buyCount * taxRare).toFixed(2)*1;
            curPrice = (buyCount * originPrice).toFixed(2)*1;
            updateBuyCountUI();
            return
        }
    }
    input = $("#buyCount").val()
    buyCount = input;
    taxCount = (buyCount * taxRare).toFixed(2)*1;
    curPrice = (buyCount * originPrice).toFixed(2)*1;
    updateBuyCountUI()
});
startCounter()
function startCounter() {
    var remainClass = $('.remainTime'), times = [], endTimeClass = $('.tgEndTime'),recordIdClass=$('.recordId');
    for (var i = 0; i < remainClass.length; i++) {
        var miliTimeStamp = $(remainClass[i]).text(), tgEndTimeStamp = $(endTimeClass[i]).val();
        /*var autoChengtuan= $('.autoChengtuan').val(),recordId=$(recordIdClass[i]).val();*/
        var timeStamp = Math.round(miliTimeStamp / 1000);
        tgEndTimeStamp = Math.round(tgEndTimeStamp / 1000);
        times.push(timeStamp);

        setInterval(function () {
            times.forEach(function (createTime, index) {
                var currentTime = Math.round(new Date().getTime() / 1000), endTime = createTime + 24 * 3600;
                var dateTime = ''
                if (endTime > tgEndTimeStamp)
                    dateTime = timeStamp2Datetime(tgEndTimeStamp - currentTime)
                else
                    dateTime = timeStamp2Datetime(endTime - currentTime)
                $($('.remainTime')[index]).text(dateTime)
                createTime--;
                /*if(autoChengtuan=='false'&&('00:00:00'.equals(dateTime))&& r>=1){
                    //自动成团
                        $.ajax({
                            url: "http://localhost:8082/GroupBuying/autoChengTuan/"+recordId,
                            type: "POST",
                            dataType:'json',
                            success: function(data) {

                            },
                            error: function(data){

                            }
                        });
                }*/
            })
        }, 200)
    }
}
function timeStamp2Datetime(spaceTimeStamp) {
    if (spaceTimeStamp <= 0){
        return '00:00:00'
    }
    var hours = Math.floor(spaceTimeStamp / 3600), minutes = Math.floor((spaceTimeStamp - 3600 * hours) / 60),
        seconds = spaceTimeStamp - hours * 3600 - minutes * 60;
    if (hours < 10)
        hours = '0' + hours;
    if (minutes < 10)
        minutes = '0' + minutes
    if (seconds < 10)
        seconds = '0' + seconds
    return hours + ':' + minutes + ':' + seconds
}
function submitNormalOrder() {
    if ( $('.js_productList').find('li.on').length == 0 ){
        TipLayer("请选择商品规格！");
        return;
    }else{
        var productAndquantity = curProductId + ',' + buyCount
        window.location.href = '/shop/pay/buynow?productIdAndquantity=' + productAndquantity
    }
}

// --------------------2018年5月8日17:48:44--lou---------------------------------
// 全额保障详细内容的显示内容判断，以及右上角图标选择性显示
var promiseCon = $('.js_promise-tag li');
var selectTemp = [];
var promiseDea = $('.js_coverage_quality p');
promiseCon.each(function(i, e) {
    selectTemp.push(e.getAttribute('data-priority'));
});
promiseDea.each(function(index, ele) {
    if (selectTemp.indexOf(ele.getAttribute('data-priority')) == -1 ) {
        ele.parentNode.style.display = "none";
    }
});
// 全额保障  选择性显示哪些左下角图标
// 以上变量表示字符优先级，priority=1的优先级最高
var selectImg = $('.js_selctImg img');
if (selectTemp.indexOf("1") > -1) {
    selectImg.attr('src', selectImg.data('src0'))
} else if (selectTemp.indexOf("2") > -1) {
    selectImg.attr('src', selectImg.data('src1'))
} else if (selectTemp.indexOf("3") > -1) {
    selectImg.attr('src', selectImg.data('src2'))
} else {
    selectImg.attr('src', selectImg.data('src3'))
}
// 自定义属性data-src表示对应字符的图标，src0对应的是priority0的值，以此类推
